package main

import (
	"mei_go/communication"
	"strconv"
	"sync"
)

type DebugMsg struct {
	Cmd    string `json:"cmd" default:"debug"`
	Value  string `json:"value"`
	Result string `json:"-"`
}

func (msg *DebugMsg) Receive() {
	println("调测信息：", msg.Value)
	msg.Result = msg.Value
}

func testSend(client *communication.Client) {
	msg := DebugMsg{
		Value: "test",
	}
	client.Send(&msg)
	println(msg.Result)
}

func testListen(client *communication.Client) {
	client.Subscribe(&DebugMsg{})
	client.Listen()
}

func testAsync() {
	wg := sync.WaitGroup{}
	n := 2
	wg.Add(n)
	f := func() {
		communication.WithAsyncClient("debug", func(client communication.AsyncClient) {
			for i := range 3 {
				ret := client.Send(strconv.Itoa(i)).(string)
				println(ret)
			}
			wg.Done()
		})
	}
	for range 2 {
		go f()
	}
	wg.Wait()
	println("end")
}

func main() {
	client := communication.GetClientInstance()
	defer client.Close()
	go testAsync()
	go testSend(client)
	testListen(client)
}
